package umn.angrypenguins.monopoly;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import umn.angrypenguins.monopoly.models.GameInstance;
import umn.angrypenguins.monopoly.models.PlayerInstance;
import umn.angrypenguins.monopoly.webservice.WebServiceClient;

public class CreateActivity extends Activity implements View.OnClickListener{

    class CreateGameTask extends AsyncTask<GameInstance, GameInstance, GameInstance> {

        @Override
        protected GameInstance doInBackground(GameInstance... game1) {
            return WebServiceClient.createGame(game1[0]);
        }

        protected void onPostExecute(GameInstance createdGame) {
            super.onPostExecute(createdGame);
            //newContact = contact2;

            if( createdGame == null )
                Log.i( "CreateActivity.CreateGameTask.onPostExecute", "createdGame is NULL" );
            
            Log.i("CreateActivity.CreateGameTask.onPostExecute", "Created game name = " + createdGame.getName());

            presentInfoView(createdGame);
        }
    }

    private void presentInfoView(GameInstance newGame) {
        //go back to Info view
        Bundle bundle = new Bundle();
        bundle.putSerializable("gameInstance", newGame);
        Intent intent = new Intent(this, WaitingActivity.class);
        intent.putExtras(bundle);
        startActivity(intent);
        //overridePendingTransition( R.anim.slide_in_right, R.anim.slide_out_right );
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.create);
        ToolbarConfig toolbarConfig = new ToolbarConfig(this, "Create Game");
        toolbarConfig.getToolbarBackButton().setText( "Back" );

        findViewById(R.id.create_game_button).setOnClickListener(this);
        findViewById(R.id.toolbar_back_button).setOnClickListener(this);
    }

    public void onClick(View view) {
        if (view.getId() == R.id.create_game_button) {
            EditText gameName = (EditText)findViewById(R.id.c_game_text);
            EditText playerName = (EditText)findViewById(R.id.c_name_text);
            EditText amount = (EditText)findViewById(R.id.amount_field);

            GameInstance newGame = new GameInstance(gameName.getText().toString());
            PlayerInstance creator = new PlayerInstance(playerName.getText().toString(), Double.valueOf(amount.getText().toString()));
            newGame.setCreator(creator);

            Log.i("CreateActivity", "creating a new game");
            new CreateGameTask().execute(newGame);
            /*
            Bundle bundle = new Bundle();
            bundle.putSerializable("playerInstance", new PlayerInstance(playerName.getText().toString(), Double.valueOf(amount.getText().toString())));
            Intent intent = new Intent();
            intent.setClass(this, WaitingActivity.class);
            intent.putExtras(bundle);
            startActivity(intent);
            */
        }
        else if (view.getId() == R.id.toolbar_back_button) {
            Intent intent = new Intent(this, GameView.class);
            startActivity(intent);
        }
    }
}
